package com.zcf.common.yunos.qiniuyun;

import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.processing.OperationManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;

public class UploadUtil {
	// 封装 七牛云里的 AK/SK 存储空间名称
	public static final String ACCESSKEY = "A7d5-Vw0daKGm770lnD9i7E_uyw7L0vCJWxtKABo";
	public static final String SECRETKEY = "HCaomkrZa8NB6LDeW2THQg0byuSvdxcsXiZGOVzF";
	// 存储空间名
	public static final String BUCKETNAME = "zaichufa";
	// 外链地址
	public static final String DOMAIN = "pbyatzk15.bkt.clouddn.com";
	// 默认不指定key的情况下，以文件内容的hash值作为文件名
	public static String key = null;

	/**
	 * 上传文件返回在线的url
	 * 
	 * @param file
	 * @return
	 */
	public static String qiniuUpload(MultipartFile file) {
		// 根据存放的机房 选择对象 这里自动选择
		Configuration cfg = new Configuration(Zone.autoZone());
		UploadManager uploadManager = new UploadManager(cfg);
		// 用户新建
		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		// 上传的空间
		String uploadToken = auth.uploadToken(BUCKETNAME);
		String qiniuUrl = "";
		try {
			// 开始上传
			Response qiniuresponse = uploadManager.put(file.getInputStream(), key, uploadToken, null, null);
			// 解析上传成功的结果
			DefaultPutRet putRet = new Gson().fromJson(qiniuresponse.bodyString(), DefaultPutRet.class);
			qiniuUrl = DOMAIN + "/" + putRet.key;
			// System.out.println("文件地址:" + qiniuUrl);
			// 调用获取截图的方法获取截图
			UploadUtil.screenshots(BUCKETNAME, putRet.key);
		} catch (QiniuException e) {
			Response r = e.response;
			System.err.println(r.toString());
			try {
				System.err.println(r.bodyString());
			} catch (QiniuException ex2) {

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return qiniuUrl;
	}

	/**
	 * 七牛视频截图方法
	 */
	public static void screenshots(String bucket, String key) {
		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		Configuration cfg = new Configuration(Zone.autoZone());
		// 新建一个OperationManager对象
		OperationManager operater = new OperationManager(auth, cfg);
		// 设置转码操作参数
		String fops = "vframe/jpg/offset/1/w/480/h/360/rotate/auto";
		// 可以对转码后的文件进行使用saveas参数自定义命名，当然也可以不指定文件会默认命名并保存在当前空间。
		// 获取的图片路径是CF+之前视频的key值
		String urlbase64 = UrlSafeBase64.encodeToString(bucket + ":CF" + key);
		String pfops = fops + "|saveas/" + urlbase64;
		// 设置pipeline参数
		StringMap params = new StringMap().putWhen("force", 1, true).putNotEmpty("pipeline", null);
		try {
			String persistid = operater.pfop(bucket, key, pfops, params);
			// 打印返回的persistid
			System.out.println(persistid);
		} catch (QiniuException e) {
			// 捕获异常信息
			Response r = e.response;
			// 请求失败时简单状态信息
			System.out.println(r.toString());
			try {
				// 响应的文本信息
				System.out.println(r.bodyString());
			} catch (QiniuException e1) {
				// ignore
			}
		}

	}

}
